<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\ConfigEnum;
use app\enum\OceanEnum;
use app\model\OeSchemaUrlModel;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class OeSourcePicListWork extends BaseWork
{
    public const REDIS_TASK_KEY = ConfigEnum::CACHE_CONFIG[OceanEnum::LIST_CONFIG_KEY]['source_pic'];

    protected function configure()
    {
        // 指令配置
        $this->setName('oe_source_pic_list_work');
        // 设置参数
        $this->setDescription('生成巨量直达链接图片队列');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        $this->getOutputMessage('巨量引擎-生成巨量直达链接图片队列开始');
        try {
            $redis = Cache::store('redis')->handler();
            $schemaUrl = self::getSourceUrl();
            if ($schemaUrl) {
                foreach ($schemaUrl as $val) {
                    $redis->rPush(self::REDIS_TASK_KEY, $val->getAttr('account_id') . '_' . $val->getKey() . '_' . 1);
                    $this->getOutputMessage('巨量引擎-生成巨量直达链接图片队列成功,id:' . $val->getKey());
                }
            }
        } catch (\Exception $e) {
            echo $e->getMessage() . PHP_EOL;
            echo $e->getFile() . '[' . $e->getLine() . ']' . PHP_EOL;
            echo $e->getTraceAsString() . PHP_EOL;
        }
        $this->getOutputMessage('巨量引擎-生成巨量直达链接图片队列结束');
    }

    /*
     * 获取未同步图片的直达链接
     */
    private function getSourceUrl()
    {
        return (new OeSchemaUrlModel())->where('is_sync_pic', 0)->select();
    }
}
